package ca.cbc.msb.mq;

import javax.jms.JMSException;
import javax.naming.NamingException;

import ca.cbc.msb.persistence.FailedMediaPersistence;
import ca.cbc.msb.persistence.MediaPersistence;
import ca.cbc.msb.persistence.ProcessingStatusPersistence;
import ca.cbc.msb.util.FTPUtil;
import ca.cbc.msb.util.ProgressReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.cbc.medialib.entity.Media;
import ca.cbc.medialib.entity.enums.ProcessStatus;
import ca.cbc.medialib.util.ApplicationContextUtil;
import ca.cbc.msb.entity.MSBMessage;
import ca.cbc.msb.enums.QueueEnum;
import ca.cbc.msb.gsa.GSAService;
import ca.cbc.msb.mobilemedia.MobileMediaService;
import ca.cbc.msb.mobilemedia.YospaceServiceImpl;

/**
 * 
 * JMS implementation of MessageHandler for publishing media release to GSA and/or yoSpace.
 * 
 * @author w. w
 */
public class NewReleaseMessageHandler implements MessageHandler {

	private Logger log = LoggerFactory.getLogger(this.getClass());
    private GSAService gsaService;
    private MobileMediaService mobileMediaService;
    private MediaPersistence mediaPersistence;
    private FailedMediaPersistence failedMediaPersistence;
    private ProcessingStatusPersistence processingStatusPersistence;

    public NewReleaseMessageHandler() {
    	gsaService = new GSAService();
    	mobileMediaService = new YospaceServiceImpl();
    	mediaPersistence = ApplicationContextUtil.instance.getApplicationContext().getBean(MediaPersistence.class);
    	failedMediaPersistence = ApplicationContextUtil.instance.getApplicationContext().getBean(FailedMediaPersistence.class);
    	processingStatusPersistence = ApplicationContextUtil.instance.getApplicationContext().getBean(ProcessingStatusPersistence.class);
    }

    @Override
    public void handle(MSBMessage message, MessageUtil messageUtil) throws NamingException,
            JMSException {
        log.debug(">>>NewReleaseMessage Received ({})", message);
        Media media = message.getMedia();
        boolean isSumbitToYospace = false;

        try{
        	//0. commit received message
        	messageUtil.commit();

        	//1. sumbit to GSA
	        gsaService.submitToGSA(message);
	        
	        //2. yospace?
	        if(media.getAvailableInMobile().equalsIgnoreCase("true") && FTPUtil.isFile(media.getOriginalFileName())) {
	        	//New Release Media, submit to Yospace
	        	processingStatusPersistence.changeProcessingFlag(message, ProcessStatus.UPLOAD_TO_MOBILE);
	        	mobileMediaService.addMedia(message);
	        	messageUtil.submitMessage(QueueEnum.YospaceVerificationQueue, message);
	        	messageUtil.commit(); //commit for submit message
	            isSumbitToYospace = true;
	            ProgressReport.instance.statusReport(message, "Submitted to Yospace, Waiting For verification");
	            log.info("NewReleaseMediaMessage({}) submit to yospace.", message);
	        } else { //3. save or update to db
	        	mediaPersistence.saveOrMerge(media);
	        }
        } catch (Exception e) {
        	failedMediaPersistence.save(media);
        	log.error("Unkonw error when processing new media, abort further action.", e);
        } finally { //will remove processing flag as long as not submit to yospace
        	if(!isSumbitToYospace) {
        		processingStatusPersistence.removeProcessingFlag(message);
        	}
        }
    }
}
